{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-09T03:33:34.397860Z",
     "start_time": "2025-04-09T03:33:34.317808Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import random"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:34:13.119879Z",
     "start_time": "2025-04-09T03:34:13.114145Z"
    }
   },
   "cell_type": "code",
   "source": [
    "f = np.array([1,2,3,4,5,6,7,8,9,10],dtype=np.int16)\n",
    "print(f.itemsize)\n",
    "print(f.dtype)"
   ],
   "id": "4a275fdd06000f78",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:41:19.658347Z",
     "start_time": "2025-04-09T03:41:19.653945Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(round(random.random(),2))\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "print(arr)\n",
    "print(arr.itemsize)\n",
    "print(arr.dtype)\n",
    "print(np.round(arr,2))"
   ],
   "id": "fd5556ec6029f45e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.74\n",
      "[0.76748461 0.3069851  0.81405755 0.67796054 0.4616394  0.35618777\n",
      " 0.90823278 0.17831577 0.4232066  0.55866971]\n",
      "8\n",
      "float64\n",
      "[0.77 0.31 0.81 0.68 0.46 0.36 0.91 0.18 0.42 0.56]\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:53:44.910592Z",
     "start_time": "2025-04-09T03:53:44.905554Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#轴运算，轴是哪个，就消掉哪个轴，下面代码是（2,3），因此轴是0，剩下3，轴是1，剩下2\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(a)\n",
    "print(\"-\"*20)\n",
    "print(np.sum(a,axis=0))\n",
    "print(\"-\"*20)\n",
    "print(np.sum(a,axis=1))\n",
    "print(\"-\"*20)\n",
    "print(np.sum(a))"
   ],
   "id": "cc5be1726848c511",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------\n",
      "[5 7 9]\n",
      "--------------------\n",
      "[ 6 15]\n",
      "--------------------\n",
      "21\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T04:09:19.381321Z",
     "start_time": "2025-04-09T04:09:19.375239Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#切片\n",
    "t1=np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print(\"-\"*20)\n",
    "print(t1[1])\n",
    "print(\"-\"*20)\n",
    "print(t1[1:])\n",
    "print(\"-\"*20)\n",
    "print(t1[1:3,:])\n",
    "print(\"-\"*20)\n",
    "print(t1[[0,2,3]])#取0，2，3行\n",
    "print(\"-\"*20)\n",
    "print(t1[1,2]) #第二列第三个数字\n",
    "print(\"-\"*20)\n",
    "print(t1[[0,1,1],[0,1,3]])\n",
    "print(\"-\"*20)\n",
    "print(t1[1:3,1:4])\n",
    "print(\"-\"*20)\n",
    "print(t1[:,[0,2,3]])#取0，2，3列"
   ],
   "id": "b25bb087cb0a82c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[ 6  7  8  9 10 11]\n",
      "--------------------\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "--------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "8\n",
      "--------------------\n",
      "[0 7 9]\n",
      "--------------------\n",
      "[[ 7  8  9]\n",
      " [13 14 15]]\n",
      "--------------------\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T08:35:42.052341Z",
     "start_time": "2025-04-09T08:35:42.045564Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#添加和删除\n",
    "a = np.arange(10)\n",
    "b= a.reshape(2,5)\n",
    "print(b)\n",
    "print(\"-\"*20)\n",
    "c = np.append(b,[11,12]) #直接展平\n",
    "print(c)\n",
    "d = np.append(b,[[11,12,13,14,15]],axis=0)#往哪个轴添加，轴数会加一\n",
    "print(\"-\"*20)\n",
    "print(d)\n",
    "print(\"-\"*20)\n",
    "e = np.append(b,[[11,12,13],[16,17,18]],axis=1)\n",
    "print(e)\n",
    "print(\"-\"*20)\n",
    "print(np.delete(b,0))\n",
    "print(\"-\"*20)\n",
    "print(np.delete(b,[0],axis=0))\n",
    "print(\"-\"*20)\n",
    "print(np.delete(b,1,axis=1))"
   ],
   "id": "8277b7b500e0b2f9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "--------------------\n",
      "[ 0  1  2  3  4  5  6  7  8  9 11 12]\n",
      "--------------------\n",
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [11 12 13 14 15]]\n",
      "--------------------\n",
      "[[ 0  1  2  3  4 11 12 13]\n",
      " [ 5  6  7  8  9 16 17 18]]\n",
      "--------------------\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "--------------------\n",
      "[[5 6 7 8 9]]\n",
      "--------------------\n",
      "[[0 2 3 4]\n",
      " [5 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T08:52:36.146600Z",
     "start_time": "2025-04-09T08:52:36.141645Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#求平均值，沿着某个轴求。写哪个轴，哪个轴消失\n",
    "#写哪个轴，哪个轴变化\n",
    "t = np.arange(30).reshape(10,3)\n",
    "print(t)\n",
    "print(\"-\"*20)\n",
    "print(np.mean(t,axis=0))\n"
   ],
   "id": "7442b765f4f989bf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]\n",
      " [12 13 14]\n",
      " [15 16 17]\n",
      " [18 19 20]\n",
      " [21 22 23]\n",
      " [24 25 26]\n",
      " [27 28 29]]\n",
      "--------------------\n",
      "[13.5 14.5 15.5]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T09:05:44.258470Z",
     "start_time": "2025-04-09T09:05:44.252791Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#堆叠,没懂\n",
    "a = np.arange(10).reshape(2,5)\n",
    "print(a)\n",
    "print(\"-\"*20)\n",
    "b = np.arange(10,20).reshape(2,5)\n",
    "print(b)\n",
    "print(\"-\"*20)\n",
    "c = np.stack((a,b),axis=0)#多加了一个轴\n",
    "print(c)\n",
    "print(\"-\"*20)\n",
    "d = np.stack((a,b),axis=1)\n",
    "print(d)"
   ],
   "id": "9279cf1c7bf8ad37",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "--------------------\n",
      "[[10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n",
      "--------------------\n",
      "[[[ 0  1  2  3  4]\n",
      "  [ 5  6  7  8  9]]\n",
      "\n",
      " [[10 11 12 13 14]\n",
      "  [15 16 17 18 19]]]\n",
      "--------------------\n",
      "[[[ 0  1  2  3  4]\n",
      "  [10 11 12 13 14]]\n",
      "\n",
      " [[ 5  6  7  8  9]\n",
      "  [15 16 17 18 19]]]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T09:11:54.863623Z",
     "start_time": "2025-04-09T09:11:54.858668Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#split\n",
    "a = np.arange(10).reshape(2,5)\n",
    "print(a)\n",
    "print(\"-\"*20)\n",
    "print(\"将数组分为两个大小相等的子数组，b是一个列表\")\n",
    "b = np.split(a,5,axis=1)\n",
    "print(b)"
   ],
   "id": "bbbb92d1e5c40066",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "--------------------\n",
      "将数组分为两个大小相等的子数组，b是一个列表\n",
      "[array([[0],\n",
      "       [5]]), array([[1],\n",
      "       [6]]), array([[2],\n",
      "       [7]]), array([[3],\n",
      "       [8]]), array([[4],\n",
      "       [9]])]\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T09:30:31.972294Z",
     "start_time": "2025-04-09T09:30:31.968348Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#nan和inf\n",
    "#nan是not a num\n",
    "a = np.array([1,2,3,4,5,np.nan,np.inf])\n",
    "print(np.isnan(a))\n",
    "print(np.isinf(a))\n",
    "print(np.nan == np.nan)\n",
    "print(np.count_nonzero(a!=a))d"
   ],
   "id": "2fa817d8942208fa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False False False False  True False]\n",
      "[False False False False False False  True]\n",
      "False\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 43
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
